package table.col;

import java.util.Iterator;
import java.util.List;

import table.Col;
import table.Row;
import table.node.BaseNode;
import table.node.FormulaNode;

public class FormulaCol extends Col {
	private String formula;
	public void setFormula(String formula) {
		this.formula = formula;
	}

	public String getFormula() {
		return formula;
	}

	public void setDataByCols(List<Col> cols){
		
		if( 0 < cols.size() && cols.get(0) != null && cols.get(0).getDataSize() > 0){
			if( 0 < cols.size() && cols.get(0) != null && cols.get(0).getDataSize() > 0){
				FormulaNode formulaNode = FormulaNode.formulaStringToNode(this.getFormula());
				int dataSize = cols.get(0).getDataSize();
				for(int i=0; i < dataSize; i++){
					BaseNode dataNode = new BaseNode();
					try{
						Row dataRow = new Row();
						for(Iterator<Col> it = cols.iterator();it.hasNext();){
							Col tCol = it.next(); 
							dataRow.setValueByName(tCol.getName(), tCol.getValue(i));
						}
						dataNode.setNodeValue(formulaNode.toDouble(dataRow));
					}catch(Exception e){
						dataNode.setNodeValue("Error!");
					}
					this.addData(dataNode);
				}
			}
		}
	}
	
}
